package com.cs.pk.codestriker;

import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import com.cs.pk.utils.ClearCaseException;
import com.cs.pk.utils.UtilRepository;

public class ModifiedFile extends AbstractPopulateList {

	private Table ft = null;
	private String viewDir = null;
	public ModifiedFile(Object component) {
		super(component);
		ft = (Table)component;
	}

	protected String filterOutput() {
		if(result!=null) {
			result = result.substring(result.indexOf("change set versions:"));
		}
		return result;
	}

	protected void populate(Object payload) throws IOException,	InterruptedException, ClearCaseException {
		String activityName = (String)payload;
		viewDir = (String)UtilRepository.getActivityRelatedViewDir(activityName);
		String changeset = runBatFile(new String[] {UtilRepository.DRIVE+":","cd "+ viewDir,"cleartool lsactivity -l "+  activityName}).trim();
		String tokens[] = changeset.split("\n");
		for (int i = 1; i < tokens.length; i++) {
			populateTable(tokens[i].trim());
		}
		
	}
	
	private void populateTable(String fileInfo) {
		String f[] = fileInfo.split("@@");
		f[0] = f[0].substring(viewDir.length()+1).trim();
		f[1] = f[1].trim();
		TableItem item = new TableItem(ft,SWT.NONE);
		item.setChecked(true);
		item.setText(1,f[0]);
		item.setText(2,f[1]);
	}

	public void clear() {
		ft.removeAll();
	}

}
